﻿using System;
using System.Linq;
using System.Web.UI.WebControls;

namespace EPiServer.Labs.MobilePack.Core
{
    public partial class Preview : System.Web.UI.Page
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            ViewType.SelectedIndexChanged += new EventHandler(ViewType_SelectedIndexChanged);
            if (!IsPostBack)
            {
                ViewType.Items.AddRange(
                    MobilePackSection.CurrentSection.Preview.Cast<DeviceElement>().Select(de => new ListItem(de.Name)).ToArray());


                InnerFrame.Attributes["src"] = Request["OrigUrl"] + "&InPreview=true";
                if (Session["PreviewType"] != null)
                {
                    ViewType.SelectedIndex = (int)Session["PreviewType"];
                    ViewType_SelectedIndexChanged(this, EventArgs.Empty);
                }
            }
        }

        void ViewType_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["PreviewType"] = ViewType.SelectedIndex;

            var itm=MobilePackSection.CurrentSection.Preview.Cast<DeviceElement>().Where(de => de.Name == ViewType.SelectedValue).FirstOrDefault();
            InnerFrame.Attributes["class"] = itm.Style;
            if (!string.IsNullOrEmpty(itm.Image))
            {
                backimg.Visible = true;
                backimg.Src = itm.Image;
            }
            else
            {
                backimg.Visible = false;
                backimg.Src = string.Empty;
            }
        }
    }
}